package com.xiaozhi.utils;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class MybatisAutoGenerator {

    public static void main(String[] args) {

        String[] arr={"classes","course","discussion","exam","exam_course","exam_student_teacher","question",
                "question_bank","question_bank_exam","question_bank_question","question_bank_teacher",
                "reply","student","study_resource","student_course","student_teacher_classes","student_wrong_question",
                "teacher","wrong_question"};

        for (String s : arr) {
            create(s);
        }

    }

    public static void create(String tableName){
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/xiaozhi?useSSL=true&useUnicode=true&characterEncoding=UTF-8" +
                "&serverTimezone=Asia/Shanghai", "root", "root")
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("烟尘") // 设置作者
                            // .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(System.getProperty("user.dir") + "\\src\\main\\java") // 指定输出目录
                            .commentDate("yyyy-MM-dd")  //调整注释日期时间格式
                            .dateType(DateType.TIME_PACK)  //时间策略
                            //.enableSwagger() //开启 swagger 模式
                            .disableOpenDir()   //禁止打开输出目录
                            .build();
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.parent("com.xiaozhi") // 设置父包名
                            // 设置mapperXml生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.mapper, System.getProperty("user.dir")+
                                    "\\src\\main\\java\\com\\xiaozhi\\mapper"))
                            .build();
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(tableName)                  // 设置需要生成的表名
                            .addTablePrefix("t_", "c_")         // 设置过滤表前缀
                            //entity 配置策略
                            .entityBuilder()
                            .enableLombok()                     // 开启Lombok
                            .versionColumnName("version")       // 标注乐观锁字段
                            .logicDeleteColumnName("deleted")   // 说明逻辑删除（软删除）是哪个字段
                            .enableTableFieldAnnotation()       // 属性上加说明注解
                            //mapper 策略配置
                            .mapperBuilder()
                            .superClass(BaseMapper.class)       // 继承哪个父类
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()           // @mapper 开启
                            .formatXmlFileName("%sMapper")      // xml 名
                            // service 配置策略
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            // controller 策略配置
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()                  // 开启 RestController
                            .build();

                })
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                //.templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
